<?php

namespace App\Http\Controllers\User;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Http\Service\User\UserService;
use Session,Config;


class LoginController extends Controller
{
    private $index = 'User.login';

    public function login()
    {
        return view($this->index);
    }


    /**
     * @param Request $request
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector|string
     * 用户的登录
     */
    public function do_login(Request $request)
    {
        $msg=array();//定义返回信息数组
        $where = array();

        //   获取输入信息
        $where['agent_account']  = $request->input('agent_account');
        $where['password'] = $request->input('password');

        $userService = new UserService();

        $data = $userService->login($where);




        if($data['code']==0){
            //用户保存在Session
            $data = $data['data'];

            Session::put(Config::get('custom.setting.MEMBER_TOKEN'),$data['token']);
            Session::put(Config::get('custom.setting.MEMBER_AGENT'),$data['agent']);

            return redirect('/index');
        }else{
            echo '<script>javascript:history.back();alert("'.$data['msg'].'！");</script>';
        }

    }


    /**
     * 用户退出登录
     */
    public function logout()
    {
        Session::forget(Config::get('custom.setting.MEMBER_TOKEN'));
        Session::forget(Config::get('custom.setting.MEMBER_AGENT'));
        return redirect('/');
    }




}
